using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.UIElements.Experimental;

public class MusicMgr : Singleton<MusicMgr>
{
    private float bkVolume = 0.5f;
    private AudioSource bkMusic = null;

    private float soundVolume = 0.5f;
    private GameObject soundObj = null;
    private List<AudioSource> soundList = new List<AudioSource>();

    public MusicMgr()
    {
        MonoMgr.GetInstance().AddUpdateListener(Update);
    }

    /// <summary>
    /// 修改背景音乐音量
    /// </summary>
    /// <param name="volume"></param>
    public void ChangeBkVolume(float volume)
    {
        bkVolume = volume;
        if (bkMusic == null)
            return;
        bkMusic.volume = bkVolume;
    }

    /// <summary>
    /// 播放背景音乐
    /// </summary>
    /// <param name="name"></param>
    public void PlayeBKMusic(string name)
    {
        if (bkMusic == null)
        {
            GameObject obj = new GameObject();
            obj.name = name;
            bkMusic = obj.AddComponent<AudioSource>();
        }
        ResMgr.GetInstance().LoadAsync<AudioClip>("Music/BK/" + name, (audio) =>
        {
            bkMusic.clip = audio;
            bkMusic.loop = true;
            bkMusic.volume = bkVolume;
            bkMusic.Play();
        });
    }

    /// <summary>
    /// 暂停背景音乐
    /// </summary>
    public void PauseBkMusic()
    {
        if (bkMusic == null)
            return;
        bkMusic.Pause();
    }

    public void UnPauseBkMusic()
    {
        if (bkMusic == null)
            return;
        bkMusic.UnPause();
    }

    /// <summary>
    /// 停止背景音乐
    /// </summary>
    /// <param name="name"></param>
    public void StopBKMusic()
    {
        if (bkMusic == null)
            return;
        bkMusic.Stop();
    }

    /// <summary>
    /// 修改音效音量
    /// </summary>
    /// <param name="volume"></param>
    public void ChangeSoundVolume(float volume)
    {
        soundVolume = volume;
        if (soundObj == null)
            return;
        for (int i = 0; i < soundList.Count; i++)
            soundList[i].volume = soundVolume;
    }

    /// <summary>
    /// 播放音效
    /// </summary>
    /// <param name="name"></param>
    public void PlayeSound(string name, bool isLoop = false, UnityAction<AudioSource> callback = null)
    {
        if (soundObj == null)
        {
            soundObj = new GameObject();
            soundObj.name = "Sound";
        }
        ResMgr.GetInstance().LoadAsync<AudioClip>("Music/Sound/" + name, (clip) =>
        {
            AudioSource source = soundObj.AddComponent<AudioSource>();
            source.clip = clip;
            source.volume = soundVolume;
            source.loop = isLoop;
            source.Play();
            soundList.Add(source);
            callback?.Invoke(source);
        });
    }

    /// <summary>
    /// 暂停音效
    /// </summary>
    /// <param name="audio"></param>
    public void StopSound(AudioSource source)
    {
        if (soundObj == null)
            return;
        if (soundList.Contains(source))
        {
            soundList.Remove(source);
            source.Stop();
            GameObject.Destroy(source);
        }
    }

    /// <summary>
    /// 更新定时器
    /// </summary>
    public void Update()
    {
        for (int i = soundList.Count - 1; i > 0; --i)
        {
            if (!soundList[i].isPlaying)
            {
                GameObject.Destroy(soundList[i]);
                soundList.RemoveAt(i);
            }
        }
    }
}
